package spotGUI.utils;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;

import javax.swing.JComponent;
import javax.swing.JPanel;

public class AutoResizingPanel extends JPanel implements ComponentListener{
	
	public AutoResizingPanel() {
		setPreferredSize(new Dimension (0, 0));
	}
	
	
	@Override
	public Component add(Component arg0) {
		Component c = super.add(arg0);
		int vgap = 0;
		if (getLayout() instanceof FlowLayout) {
			vgap = ((FlowLayout)getLayout()).getVgap();
		}
		setPreferredSize(new Dimension(getPreferredSize().width, getPreferredSize().height+arg0.getSize().height+vgap));
		return c;
	}
	
	@Override
	public void componentHidden(ComponentEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void componentMoved(ComponentEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void componentShown(ComponentEvent e) {
		// TODO Auto-generated method stub
		
	}
	
	@Override
	public void componentResized(ComponentEvent e) {
		setPreferredSize(new Dimension(((JComponent)e.getSource()).getSize().width, getPreferredSize().height));
	}
}
